package cn.lliiooll.config;

import cn.lliiooll.HelpMe;
import cn.lliiooll.util.Utils;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Config {


    public static List<String> list;// 帮助列表
    public static String head;// 头部
    public static String last;// 尾部
    public static boolean console;// 控制台屏蔽
    public static boolean papi;// papi支持
    public static int line;// 一次输出的行数
    public static final File configFile = new File(HelpMe.instance.getDataFolder(), "config.yml");
    public static final File subDir = new File(HelpMe.instance.getDataFolder(), "subs");
    public static FileConfiguration config;
    private static final Map<String, SubConfig> subs = new HashMap<>();

    public static boolean reload() {
        if (!configFile.exists()) save();
        Config.config = new YamlConfiguration();
        try {
            config.loadFromString(Utils.parseNotes2Key(configFile));
            Config.list = config.getStringList("help.list");
            Config.head = config.getString("help.head", "&b==========&e[ &7帮助 &e]&b==========");
            Config.last = config.getString("help.last", "剩余 $now$/$all$");
            Config.console = config.getBoolean("console", true);
            Config.papi = config.getBoolean("papi", true);
            Config.line = config.getInt("line", 5);
            return true;
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean save() {
        if (!configFile.exists()) HelpMe.instance.saveDefaultConfig();
        if (Config.config == null) reload();
        try {
            config.set("help.list", Config.list);
            config.set("help.head", Config.head);
            config.set("help.last", Config.last);
            config.set("console", Config.console);
            config.set("papi", Config.papi);
            config.set("line", Config.line);
            config.save(configFile);
            Utils.parseKey2Notes(configFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void createSub(String name) {
        if (!subDir.exists()) subDir.mkdirs();
        SubConfig sub = new SubConfig(name, subDir);
        sub.create();
        subs.put(name, sub);
    }

    public static SubConfig getSub(String arg) {
        return subs.get(arg);
    }

    public static List<String> getAllSub() {
        List<String> ls = new ArrayList<>();
        ls.addAll(subs.keySet());
        return ls;
    }

    public static void saveAllSub() {
        for (SubConfig sub : subs.values()) {
            sub.save();
        }
    }

    public static boolean reloadAllSub() {
        for (SubConfig sub : subs.values()) {
            sub.reload();
        }
        return true;
    }

    public static boolean hasSub(String arg) {
        return getAllSub().contains(arg);
    }

    public static void init() {
        Config.reload();
        Config.loadAllSub();
    }

    public static void loadAllSub() {
        for (File sub : subDir.listFiles()) {
            if (sub.getName().endsWith(".yml")) {
                createSub(sub.getName().split("\\.")[0]);
            }
        }
    }

    public static void removeSub(String arg) {
        SubConfig sub = getSub(arg);
        sub.remove();
        subs.remove(arg);
    }
}
